package com.PuzGame.combinenumbers.ServerCommunication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.util.Log;

import com.PuzGame.combinenumbers.Interfaces.IPostDataResponse;

public class PostDataAsyncTask extends AsyncTask <String,String,String>{

	IPostDataResponse m_callback;

	public PostDataAsyncTask(IPostDataResponse callback)
	{
		m_callback = callback;
	}

	protected void onProgressUpdate(String... values) {


	}
	protected void onPostExecute(String result)
	{
		m_callback.PostResponse(result);
		Log.i("PostDataResponse", result);
	}

	@Override
	protected String doInBackground(String... value) {

		String Url = value[0];
		String UserId = value[1];
		String Body = value[2];
		String result  ="";

		Log.i("PostData", "Url: "+Url);
		Log.i("PostData", "UserId: "+UserId);
		Log.i("PostData", "Body: "+Body);

		// Create a new HttpClient and Post Header
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(Url);

		// Execute the request
		HttpResponse response;
		try {

			httppost.addHeader("user-id", UserId);
			httppost.addHeader("Content-type", "application/json");
			httppost.addHeader("Accept", "application/json");

			httppost.setEntity(new StringEntity(Body));

			response = httpclient.execute(httppost);
			if(response.getStatusLine().getStatusCode() < 200 ||
			   response.getStatusLine().getStatusCode() >= 300	)
			{
				result = "Error: "+Url+" returned code "+response.getStatusLine().getStatusCode();
			}
			else
			{
				// Get hold of the response entity
				HttpEntity entity = response.getEntity();
				// If the response does not enclose an entity, there is no need
				// to worry about connection release

				if (entity != null) {

					// A Simple JSON Response Read
					InputStream instream = entity.getContent();
					result= convertStreamToString(instream);
					// now you have the string representation of the HTML request
					instream.close();
				}
			}


		}
		catch (Exception e)
		{
			result = "Error";
		}



		return result;
	}


	private static String convertStreamToString(InputStream is) {
		/*
		 * To convert the InputStream to String we use the BufferedReader.readLine()
		 * method. We iterate until the BufferedReader return null which means
		 * there's no more data to read. Each line will appended to a StringBuilder
		 * and returned as String.
		 */
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

}